﻿using System;
using System.Threading;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web
{
    public partial class _Ajax : SitePager
    {
        private static bool tipFlag = true;
        private Comment curComment = new Comment(); 

        /// <summary>
        /// 重载Load方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            this.Write();
            base.OnLoad(e);
        }

        /// <summary>
        /// 输出内容
        /// </summary>
        protected void Write()
        {
            if (!IsPostBack)
            {
                //Uri uri = Request.UrlReferrer;   //确保安全请求
                //if (uri != null && "http://" + uri.Host.ToLower() == PlugNT.Custom.WebConfig.SiteUrl.ToLower())
                //{
                //    string arg = (this.Request.QueryString["a"] == null) ? "" : this.Request.QueryString["a"].Trim();
                //    string qValue = (this.Request.QueryString["q"] == null) ? "" : this.Request.QueryString["q"].Trim();
                //    string responsestr = "";
                //    switch (arg)
                //    {
                //        case "commentcount":
                //            responsestr = ShowCommentCount(qValue);
                //            break;
                //        case "tip":
                //            if (tipFlag)
                //            {
                //                responsestr = ShowTip(qValue);
                //            }
                //            break;
                //        case "addcomment":
                //            responsestr = ShowAddComment();
                //            break;
                //        default:
                //            break;
                //    }
                //    this.Response.Write(responsestr);
                //}
                //else
                //{
                //    this.Response.Write("操作失败，非法请求！");
                //}


                string arg = (this.Request.QueryString["a"] == null) ? "" : this.Request.QueryString["a"].Trim();
                string qValue = (this.Request.QueryString["q"] == null) ? "" : this.Request.QueryString["q"].Trim();
                //分表id
                string tablepostfix = (this.Request.QueryString["t"] == null) ? PlugNT.Custom.WebConfig.TablePostfix : Request.QueryString["t"].Trim();
                curComment.CurPostfix = tablepostfix;
                string responsestr = "";
                switch (arg)
                {
                    case "tip":           //顶贴
                        if (tipFlag)
                        {
                            responsestr = ShowTip(qValue);
                        }
                        break;
                    case "addcomment":     //添加评论
                        responsestr = AddComment();
                        break;
                    case "commentcount":   //评论数目
                        responsestr = ShowCommentCount(qValue);
                        break;
                    default:
                        break;
                }
                this.Response.Write(responsestr);

            }
        }


        /// <summary>
        /// 顶贴
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        private string ShowTip(string sid)
        {
            tipFlag = false;
            try
            {
                Thread.Sleep(300);
                int id = int.Parse(sid);
                curComment.AddTip(id);
                tipFlag = true;
                return sid + "," + curComment.GetTipCount(id);
            }
            catch
            {
                tipFlag = true;
                return sid + ",0";
            }
        }

        /// <summary>
        /// 评论
        /// </summary>
        /// <returns></returns>
        private string AddComment()
        {
            try
            {
                //验证码
                string ernd = (this.Request.QueryString["e"] == null) ? "" : this.Request.QueryString["e"].Trim();
                if (ernd != "" && ernd.ToLower() == PlugNT.Pack.Imager.VerifyCoder.SessionName.ToLower())
                {
                    //文章id
                    string aid = (this.Request.QueryString["i"] == null) ? "" : this.Request.QueryString["i"].Trim();
                    //帖子id
                    string reid = (this.Request.QueryString["r"] == null) ? "" : this.Request.QueryString["r"].Trim();
                    //用户名
                    string username = (this.Request.QueryString["u"] == null) ? "" : this.Request.QueryString["u"].Trim();
                    //内容
                    string content = (this.Request.QueryString["c"] == null) ? "" : this.Request.QueryString["c"].Trim();

                    username = PlugNT.Common.Utils.ClearSpace(username);
                    content = PlugNT.Common.Utils.ClearSpace(content);

                    int taid = int.Parse(aid);
                    if (Sys.CheckComment(username, content))
                    {
                        if (reid != "")
                        {
                            int treid = int.Parse(reid);
                            return curComment.UpdateCommenteContent(taid, treid, username, content);
                        }
                        else
                        {
                            return curComment.AddCommentContent(taid, username, content);
                        }
                    }
                    else
                    {
                        return "评论中有不合法的关键字！";
                    }
                }
                else
                {
                    return "验证码错误或为空！";
                }
            }
            catch (Exception ex)
            {
                return "操作失败！" + ex.Message;  // +ex.StackTrace.Replace("\"", ":").Replace("'", "-");
            }
        }


        /// <summary>
        /// 显示评论数目
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        private string ShowCommentCount(string aid)
        {
            try
            {
                int artid = int.Parse(aid);
                return curComment.GetCount(artid).ToString();
            }
            catch
            {
                return "0";
            }
        }



    }
}
